package com.wuster.config;

import com.wuster.Interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig  implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;


    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截器:对登录接口和knife4j文档接口不拦截
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/account/login").excludePathPatterns("/swagger-ui.html","/swagger-resources/**","/webjars/**","/swagger-ui/**","/v2/api-docs/**","/doc.html");
    }

    // 后端解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*") // 允许所有域
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的方法
                .allowedHeaders("*") // 允许的头部
                .allowCredentials(true); // 是否允许发送Cookie
    }

}
